package com.exam.demo.controller.leave;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.exam.demo.mapper.WorkMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import  org.fh.domain.Work;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/work")
public class WorkController {

    @Autowired
    private WorkMapper baseMapper;

    @PostMapping(value = "/list")
    public  Object getList(Work work){
        HashMap<String,Object> map = new HashMap<>();
        QueryWrapper<Work> qw = new QueryWrapper<>();
        qw.like(work.getName() != null && !work.getName().isEmpty(),"name",work.getName())
                .like(work.getDpm() != null && !work.getDpm().isEmpty(),"dpm",work.getDpm())
                .eq(work.getDate()!=null && !work.getDate().isEmpty(),"date",work.getDate());
        List<Work> list = baseMapper.selectList(qw);
        map.put("list",list);
        return  map;
    }

    @PostMapping(value = "/add")
    public  Object add(Work work){
        HashMap<String,Object> map = new HashMap<>();
        Boolean success = baseMapper.insert(work) > 0;
        map.put("result",success ? "yes" :"no");
        return  map;
    }

    @PostMapping(value = "/edit")
    public  Object edit(Work work){
        HashMap<String,Object> map = new HashMap<>();
        Boolean success = baseMapper.updateById(work) > 0;
        map.put("result",success ? "yes" :"no");
        return  map;
    }

    @PostMapping(value = "/getInfo")
    public  Object getInfo(Work work){
        HashMap<String,Object> map = new HashMap<>();
        org.fh.domain.Work res = baseMapper.selectById(work.getId());
        map.put("data",res);
        return  map;
    }

    @PostMapping(value = "/del")
    public  Object del(Work work){
        HashMap<String,Object> map = new HashMap<>();
        Boolean success = baseMapper.deleteById(work.getId()) > 0;
        map.put("result",success ? "yes" :"no");
        return  map;
    }


    @PostMapping(value = "/stats")
    public  Object getStats(Work work){
        HashMap<String,Object> map = new HashMap<>();
        QueryWrapper<Work> qw = new QueryWrapper<>();
        qw.eq("date",new SimpleDateFormat("yyyy-MM-dd").format(new Date())).groupBy("start_work");
        List<Map<String,Object>> list = baseMapper.selectStats(qw);
        map.put("list",list);
        return  map;
    }



}
